package controller

import (
	"github.com/gin-gonic/gin"
	"go-demo/mvc/model"
	"go-demo/mvc/service"
	"go-demo/utils"
	"net/http"
	"strconv"
)

type QuestionController struct {
	QuestionService *service.QuestionService
}

func NewQuestionController(questionService *service.QuestionService) *QuestionController {
	return &QuestionController{QuestionService: questionService}
}

func (qc *QuestionController) GetQuestion(c *gin.Context) {
	id := c.Param("id")
	if s, err := strconv.Atoi(id); err == nil {
		vo, err := qc.QuestionService.GetVoById(s)
		if err != nil {
			utils.Fail(c, http.StatusBadRequest, err.Error())
			return
		}
		utils.Success(c, vo)
	} else {
		utils.Fail(c, http.StatusBadRequest, err.Error())
	}
}

func (qc *QuestionController) Page(c *gin.Context) {
	var pageRequest model.QuestionQueryParam
	err := utils.Jons2Type(c, &pageRequest)
	if err != nil {
		return
	}
	page, err := qc.QuestionService.Page(pageRequest)
	if err != nil {
		utils.Fail(c, http.StatusInternalServerError, err.Error())
	}
	utils.Success(c, page)
}
